package com.chlitina.store.modules.cash.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.service.CustomerService;
import com.chlitina.store.modules.base.service.StoreCareService;
import com.chlitina.store.modules.base.service.StoreProductService;
import com.chlitina.store.modules.cash.model.AllCardPayment;
import com.chlitina.store.modules.cash.model.AllCardPaymentDetail;
import com.chlitina.store.modules.cash.model.Member;
import com.chlitina.store.modules.cash.model.MemberShoppingCart;
import com.chlitina.store.modules.cash.model.ProductCardPayment;
import com.chlitina.store.modules.cash.model.ProductPayment;
import com.chlitina.store.modules.cash.model.StoreProductCart;
import com.chlitina.store.modules.cash.model.TabActive;
import com.chlitina.store.modules.cash.service.MemberService;
import com.chlitina.store.modules.cash.service.NomemberService;
import com.chlitina.store.modules.cash.service.PaymentUility;
import com.chlitina.store.modules.cash.service.StoreConsumeService;
import com.chlitina.store.modules.employee.dao.EmployeeDao;

/**
 * Crash Controller
 * 
 * @author Songsong.xu
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/cash/member")
public class PaymentCareController extends BaseController {
	@Autowired
	private StoreConsumeService storeConsumeService;
	@Autowired
	private NomemberService nomemberService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private StoreCareService storeCareService;
	@Autowired
	private StoreProductService storeProductService;
	@Autowired
	private CustomerService customerService;
	static int TAB_ID_CARDS_TIMES = 0;
	static int TAB_ID_CARDS_DURATION = 1;
	static int TAB_ID_CARDS_PRODUCT = 2;
	static int TAB_ID_CARDS_ALL = 3;

	static int TAB_ID_NEWCARDS_PAYMENT_ALLCARD = 0;
	static int TAB_ID_NEWCARDS_PAYMENT_CASH = 1;

	static int TAB_ID_PRODUCT_PAYMENT_PRODUCTCARD = 0;
	static int TAB_ID_PRODUCT_PAYMENT_ALLCARD = 1;
	static int TAB_ID_PRODUCT_PAYMENT_CASH = 2;
	
	static int TAB_ID_CARE_PAYMENT_TIMESCARD = 0;
	static int TAB_ID_CARE_PAYMENT_DURATIONCARD = 1;
	static int TAB_ID_CARE_PAYMENT_ALLCARD = 2;
	static int TAB_ID_CARE_PAYMENT_CASH = 3;

	static int TAB_ID_PAYMENT_NEWCARDS = 0;
	static int TAB_ID_PAYMENT_RECHARGE = 1;
	static int TAB_ID_PAYMENT_PRODUCT = 2;

	public static interface KEY {
		String KEY_MEMBER = "member";
		String KEY_TAB_ACTIVE = "tabActive";
		String KEY_MESSAGE = "message";
	}

	public static interface URI {
		String URI_MEMBER_CONSUME = "modules/cash/cashMemberConsume";
		String URI_MEMBER_DETAIL = "modules/cash/cashMemberDetail";
		String URI_MEMBER_SHOPPING_DETAIL = "modules/cash/shoppingDetail";
		String URI_MEMBER_SHOPPING_DETAIL_DOCARE = "modules/cash/shoppingDetailDocare";
		String URI_MEMBER_SHOPPING_DETAIL_PRODUCT = "modules/cash/shoppingDetailProduct";
		String URI_MEMBER_SHOPPING_DETAIL_RECHARGE = "modules/cash/shoppingDetailRecharge";
		String URI_MEMBER_SHOPPING_PAYMENT = "modules/cash/shoppingPayment";
		String URI_MEMBER_CARDS = "modules/cash/cashMemberCards";
		String URI_MEMBER_CARDS_NEW = "modules/cash/cashMemberCardsNew";
		String URI_MEMBER_SHOPPING_NEWCARDS = "modules/cash/cashMemberShoppingNewCards";
		String URI_MEMBER_BUY_PRODUCT = "modules/cash/cashMemberBuyProduct";
		String URI_MEMBER_DO_CARE = "modules/cash/cashMemberDoCare";
		String URI_MEMVER_SHOOPING_NEWCARDS_PAYMENT = "modules/cash/newcardsPayment";
		String URI_MEMVER_SHOOPING_PRODUCT_PAYMENT = "modules/cash/productPayment";
		String URI_MEMVER_SHOOPING_RECHARGE_PAYMENT = "modules/cash/rechargePayment";
		String URI_MEMVER_SHOOPING_CARE_PAYMENT = "modules/cash/carePayment";

	}
	 
	 
//	/**
//	 * @deprecated
//	 * @param request
//	 * @param response
//	 * @param model
//	 * @return
//	 */
//	@RequestMapping(value = { "payway/care" })
//	public String paywayCare(HttpServletRequest request,
//			HttpServletResponse response, Model model) {
//		Member member = (Member) request.getSession().getAttribute(
//				KEY.KEY_MEMBER);
//		TabActive tabActive = new TabActive();
//		model.addAttribute(KEY.KEY_TAB_ACTIVE, tabActive);
//		String parameter = request.getParameter("pid");
//		int pid = Integer.parseInt(parameter);
//		MemberShoppingCart shoppingCart = member.getShoppingCart();
//		StoreProductCart cart = null;
//		for (StoreProductCart storeProductCart : shoppingCart
//				.getStoreProducts()) {
//			if (storeProductCart.getStoreProduct().getId().longValue() == pid) {
//				cart = storeProductCart;
//				break;
//			}
//		}
//		if (cart == null) {
//			for (StoreProductCart storeProductCart : shoppingCart
//					.getSystemProducts()) {
//				if (storeProductCart.getStoreProduct().getId().longValue() == pid) {
//					cart = storeProductCart;
//					break;
//				}
//			}
//		}
//		if (cart == null) {
//			return URI.URI_MEMBER_SHOPPING_DETAIL_DOCARE;
//		}
//		cart.updatePayment();
//		member.setProductCart(cart);
//		cart.setAvailableProductCards(PaymentUility.findProductCardsForProduct(
//				member.getAvaibleProductCards(), cart.getStoreProduct().getId()
//						.longValue(), cart.getLeftToPay()));
//		cart.setAvailableAllCards(PaymentUility.findAllCardsForProduct(
//				member.getAvaibleAllCards(), cart.getLeftToPay()));
//
//		int tabID = TAB_ID_PRODUCT_PAYMENT_CASH;
//		if (!cart.getAvailableProductCards().isEmpty()) {
//			tabID = TAB_ID_PRODUCT_PAYMENT_PRODUCTCARD;
//		} else if (!cart.getAvailableAllCards().isEmpty()) {
//			tabID = TAB_ID_PRODUCT_PAYMENT_ALLCARD;
//		}
//		tabActive.setId(tabID);
//		return URI.URI_MEMVER_SHOOPING_CARE_PAYMENT;
//	}

//	/**
//	 * @deprecated
//	 * @param request
//	 * @param response
//	 * @param model
//	 * @return
//	 */
//	@RequestMapping(value = { "payway/care/cash" })
//	public String paywayCareCash(HttpServletRequest request,
//			HttpServletResponse response, Model model) {
//		Member member = (Member) request.getSession().getAttribute(
//				KEY.KEY_MEMBER);
//		TabActive tabActive = new TabActive();
//		tabActive.setId(TAB_ID_PRODUCT_PAYMENT_CASH);
//		model.addAttribute(KEY.KEY_TAB_ACTIVE, tabActive);
//		StoreProductCart productCart = member.getProductCart();
//		if (productCart == null) {
//			tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
//			return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
//		}
//
//		String cashMoney = request.getParameter("cashMoney");
//		ProductPayment payment = productCart.getPayment();
//		if (payment != null) {
//			model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，支付方式已经设置，不能重复设置");
//			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		}
//		float toPay = Float.parseFloat(cashMoney);
//		// if (toPay != productCart.getLeftToPay()) {
//		// model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，请新设置支付信息");
//		// return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		// }
//		payment = new ProductPayment();
//		payment.setCash(toPay);
//		productCart.setPayment(payment);
//
//		productCart.updatePayment();
//		model.addAttribute(KEY.KEY_MESSAGE, "支付信息已成功记录到结算单，稍后统一结算");
//		if (productCart.getLeftToPay() == 0) {
//			tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
//			return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
//		}
//		return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//	}

//	/**
//	 * @deprecated
//	 * @param request
//	 * @param response
//	 * @param model
//	 * @return
//	 */
//	@RequestMapping(value = { "payway/care/allcard" })
//	public String paywayCareAllCard(HttpServletRequest request,
//			HttpServletResponse response, Model model) {
//		Member member = (Member) request.getSession().getAttribute(
//				KEY.KEY_MEMBER);
//		TabActive tabActive = new TabActive();
//		tabActive.setId(TAB_ID_PRODUCT_PAYMENT_ALLCARD);
//		model.addAttribute(KEY.KEY_TAB_ACTIVE, tabActive);
//		StoreProductCart productCart = member.getProductCart();
//		if (productCart == null) {
//			tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
//			return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
//		}
//
//		String cardidParam = request.getParameter("allcardId");
//		if (cardidParam == null || "".equals(cardidParam)) {
//			model.addAttribute(KEY.KEY_MESSAGE, "错误的会员卡编号，请新设置支付信息");
//			return URI.URI_MEMVER_SHOOPING_NEWCARDS_PAYMENT;
//		}
//		long cardid = -1;
//		try {
//			cardid = Long.parseLong(cardidParam);
//		} catch (NumberFormatException e) {
//			model.addAttribute(KEY.KEY_MESSAGE, "错误的会员卡编号，请新设置支付信息");
//			return URI.URI_MEMVER_SHOOPING_NEWCARDS_PAYMENT;
//		}
//		String cashMoney = request.getParameter("cashMoneyCardAll");
//
//		AllCardPayment totalCard = PaymentUility.findCardAllPaymentByPaymentId(
//				member.getAvaibleAllCards(), cardid);
//		if (totalCard == null) {// 支付卡设置错误
//			model.addAttribute(KEY.KEY_MESSAGE, "错误的会员卡编号，请新设置支付信息");
//			return URI.URI_MEMVER_SHOOPING_NEWCARDS_PAYMENT;
//		}
//
//		float toPay = Float.parseFloat(cashMoney);
//		// if (toPay != productCart.getLeftToPay()) {
//		// model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，请新设置支付信息");
//		// return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		// }
//
//		ProductPayment payment = productCart.getPayment();
//		if (payment != null) {
//			model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，支付方式已经设置，不能重复设置");
//			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		}
//		if (totalCard.getMoney() + toPay > totalCard.getCard().getMoney()) {
//			model.addAttribute(KEY.KEY_MESSAGE, "余额不足，请确认支付信息");
//			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		}
//
//		payment = new ProductPayment();
//		AllCardPaymentDetail detail = new AllCardPaymentDetail();
//		detail.setCardPayment(totalCard);
//		detail.setCurrentLeftMoney(totalCard.getCard().getMoney()
//				- totalCard.getMoney());
//		//detail.addMoney(toPay);
//		payment.setAllCardPaymentDetail(detail);
//		productCart.setPayment(payment);
//		member.updateAvailableAllCards();
//		productCart.updatePayment();// 更新本产品结算信息
//		model.addAttribute(KEY.KEY_MESSAGE, "支付信息已成功记录到结算单，稍后统一结算");
//		if (productCart.getLeftToPay() == 0) {
//			tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
//			return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
//		}
//		return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//	}

	/**
	 * @deprecated
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "payway/care/timescard" })
	public String paywayCareTimesCard(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = (Member) request.getSession().getAttribute(
				KEY.KEY_MEMBER);
		TabActive tabActive = new TabActive();
		tabActive.setId(TAB_ID_PRODUCT_PAYMENT_PRODUCTCARD);
		model.addAttribute(KEY.KEY_TAB_ACTIVE, tabActive);
		StoreProductCart productCart = member.getProductCart();
		if (productCart == null) {
			tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
			return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
		}

		String cardidParam = request.getParameter("productcardId");
		long cardid = Long.parseLong(cardidParam);
		String cashMoney = request.getParameter("cashMoneyCardProduct");
//		ProductCardPayment totalPayment = PaymentUility
//				.findCardProductPaymentById(member.getAvaibleProductCards(),
//						cardid);
		ProductCardPayment detail = PaymentUility
				.findCardProductPaymentByProduct(
						productCart.getAvailableProductCards(), cardid);
//		if (totalPayment == null || detail == null) {// 支付卡设置错误
//			model.addAttribute(KEY.KEY_MESSAGE, "错误的会员卡编号，请新设置支付信息");
//			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		}

		float toPay = Float.parseFloat(cashMoney);
		if (toPay != detail.getRealCost()) {//should pay off once
			model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，请新设置支付信息");
			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
		}

		ProductPayment payment = productCart.getPayment();
		if (payment != null) {
			model.addAttribute(KEY.KEY_MESSAGE, "错误支付金额，支付方式已经设置，不能重复设置");
			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
		}
//		if (totalPayment.getMoney() + toPay > totalPayment.getCard().getMoney()) {
//			model.addAttribute(KEY.KEY_MESSAGE, "余额不足，请确认支付信息");
//			return URI.URI_MEMVER_SHOOPING_PRODUCT_PAYMENT;
//		}

		payment = new ProductPayment();
		detail.setMoney(toPay);
//		payment.setProductCardPayment(detail);
		productCart.setPayment(payment);
	//	totalPayment.addMoney(toPay);
		member.updateAvailableProductCards();
		productCart.updatePayment();// 更新本产品结算信息
		model.addAttribute(KEY.KEY_MESSAGE, "支付信息已成功记录到结算单，稍后统一结算");

		tabActive.setId(TAB_ID_PAYMENT_PRODUCT);
		return URI.URI_MEMBER_SHOPPING_DETAIL_PRODUCT;
	}
	 
}
